﻿@model AppsModel.Models.CajaApp

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@Styles.Render("~/Content/css/loading.css")

<script type="text/javascript"
    src="//caja.appspot.com/caja.js">
</script>

<div id="loadingDiv" style="margin:20px;">
    <h4 class="subheader">Loading</h4>
    <div id="floatingBarsG" style="margin-left:12px;">
        <div class="blockG" id="rotateG_01"></div>
        <div class="blockG" id="rotateG_02"></div>
        <div class="blockG" id="rotateG_03"></div>
        <div class="blockG" id="rotateG_04"></div>
        <div class="blockG" id="rotateG_05"></div>
        <div class="blockG" id="rotateG_06"></div>
        <div class="blockG" id="rotateG_07"></div>
        <div class="blockG" id="rotateG_08"></div>
    </div>
</div>
<div id="guest" style="overflow-y:hidden;overflow-x:hidden;"></div>
<div id="debug">
    <button id="debugBtn" onclick="javascript:">Debug Button</button>
</div>
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>

<!-- Functions for the userAPI -->
<script src="@Url.Content("~/content/js/cajajs/userapi.js")"></script>
<!-- Functions for the workoutAPI -->
<script src="@Url.Content("~/content/js/cajajs/workoutapi.js")"></script>
<!-- Functions for the appsAPI -->
<script src="@Url.Content("~/content/js/cajajs/appsapi.js")"></script>
<script type="text/javascript">
    function alertGreeting() {
        alert('@Model.name');
    };

    caja.initialize({
        cajaServer: 'https://caja.appspot.com/',
        debug: true
    });

    var uriPloicy = {
        rewrite: function (uri) {
            if (/http:\/\/marcusabrahamsson\.azurewebsites\.net/.test(uri) || /http:\/\/workoutapps\.azurewebsites\.net\/api\/apps/.test(uri) ||
                /https:\/\/appsapi\.azurewebsites\.net\/cajaapps\/securitytest\/index\.html/.test(uri)) {
                return uri;
            }
            else {
                console.log("Net Access denied: The requested uri( " + uri + " )is not allowed. Check the uri policy to see what uri that is allowed.");
                return undefined;
            }
        }
    };

    function markFunctionForCaja() {
        caja.markFunction(getWorkouts);
        caja.markFunction(getWorkoutById);
        caja.markFunction(postWorkout);
        caja.markFunction(deleteWorkout);

        caja.markFunction(getUser);
        caja.markFunction(getLoggedInUser);

        caja.markFunction(getApps);
        caja.markFunction(getAppsById);
    }

    function tameFunctionForCaja(CajaApi) {
        CajaApi.tamedGetWorkouts = caja.tame(getWorkouts);
        CajaApi.tamedGetWorkoutById = caja.tame(getWorkoutById);
        CajaApi.tamedpostWorkout = caja.tame(postWorkout);
        CajaApi.tameddeleteWorkout = caja.tame(deleteWorkout);

        CajaApi.tamedgetUser = caja.tame(getUser);
        CajaApi.tamedgetLoggedInUser = caja.tame(getLoggedInUser);

        CajaApi.tamedgetApps = caja.tame(getApps);
        CajaApi.tamedgetAppsById = caja.tame(getAppsById);
    }
    caja.load(document.getElementById('guest'), uriPloicy, function (frame) {
        markFunctionForCaja();
        var CajaApi = {};
        tameFunctionForCaja(CajaApi);
        frame.code('https://appsapi.azurewebsites.net/cajaapps/@Model.id/index.html',
                    'text/html')
                .api({
                    getWorkouts: CajaApi.tamedGetWorkouts, getWorkoutById: CajaApi.tamedGetWorkoutById, postWorkout: CajaApi.tamedpostWorkout,
                    deleteWorkout: CajaApi.tameddeleteWorkout, getUser: CajaApi.tamedgetUser, getLoggedInUser: CajaApi.tamedgetLoggedInUser,
                    getApps: CajaApi.tamedgetApps, getAppsById: CajaApi.tamedgetAppsById
                })
                .run(function () {
                    $('#loadingDiv').empty();
                });
    });

</script>
